<?php
	require_once("../includes/public.inc.php");
	//$url_get='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx934cb7f852f6c585&secret=94ea78cbbbd72c8a0ecbfc965a497afc';
//$json=json_decode(https_request($url_get),true);
//    	print_r($json);exit;

function getAccessToken()
{
 		$data = json_decode(file_get_contents("access_token.json"));
   	 	if ($data->expire_time < time()) {
     	$url_get='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx934cb7f852f6c585&secret=94ea78cbbbd72c8a0ecbfc965a497afc';
	  	$json=json_decode(https_request($url_get),true);
    	//print_r($json);exit;
		if (!isset($json['errmsg']))
		{
				$access_token = $json['access_token'];
			      if ($access_token) {
				        $data->expire_time = time() + 7000;
				        $data->access_token = $access_token;
				        $fp = fopen("access_token.json", "w");
				        fwrite($fp, json_encode($data));
				        fclose($fp);
			      }
		}
		else {
			echo '获取access_token发生错误：错误代码,微信返回错误信息：';
		}
    } else {
      $access_token = $data->access_token;
    }
	
	return $access_token;
}

function https_request($url, $data = null)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
       curl_setopt($curl, CURLOPT_POST, 1);
       curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

$access_token=getAccessToken();	//echo $access_token;exit;
$a = json_encode(array("type"=>'news',"offset"=>0,"count"=>20));

$a_url = 'https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token='.$access_token;
$result = https_request($a_url, $a);
$arr = json_decode($result,true);
//print_r();exit;
$record = array();
$i=0;
foreach ($arr as $ar)
{
	
	if(is_array($ar)) //add
	{ 
		foreach ($ar as $a)
		{
			if(is_array($a)) //add
			{
				foreach ($a as $acn)
				{
					if (is_array($acn))
					{
						$record[] = $acn['news_item'];
					}
					
				}
			}
		}
	}
		
	
}
$re =array();
foreach ($record as $r)
{
	foreach ($r as $rr)
	{
		unset($rr['content']);
		unset($rr['content_source_url']);
		unset($rr['digest']);
		$re[] = $rr;
	}
}
//print_r($re);exit;
?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<meta name="viewport" content="user-scalable=yes, width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>今日分享</title>
<link rel="stylesheet" type="text/css" href="css/huodong.css" media="all">
</head>
<body>
<div class="pz-css">
	<ul class="top-tool">
		<li class="title">今日分享</li>
		<li class="back"><a href="javascript:history.go(-1)"></a>
		</li><li class="home"><a href="index.php?type=<?=$_GET['type']?>"></a></li>
	</ul>
		<div class="list" style="padding: 5px;">
      <?foreach ($re as $rt):?>  
		<a href="<?=$rt['url']?>" style="background: url('ico.png') no-repeat scroll right center rgb(255, 255, 255);" class="item">
			<h4><?=$rt['title']?></h4>
			<h5><?=$rt['title']?></h5>
		</a>
	  <?endforeach;?>
	
		</div>
		
	<div class="span12"><button type="button" class="red" onclick="javascript:history.go(-1)">返回</button></div>
</div>
</body>
</html>